package pagePrint;

import java.io.IOException;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

import javax.servlet.jsp.JspWriter;

import channel.Channel;
import database.Database;

public class pagePrintThat {
	//print channel message while user create a THAT channel in "that.jsp"
	public static void Print(JspWriter out, int id)
	{
		try {
			out.println("<div><div class='step_title'>");
			out.print("<img src=");
			
			Connection con = Database.getConnection();
			Statement stmt = con.createStatement();
			
			ResultSet rs = stmt.executeQuery(String.format(
						"select tag, pic_url " +
						"from channel " +
						"where id = %d",
						id));
			
			rs.next();
			out.print("'" + rs.getString("pic_url") + "'");
			out.print(" title = ");
			out.println("'" + rs.getString("tag") + "' style='height:30; width:30'/>");
			out.println("<span class='step_t'>Choose a trigger</span>" + 
						"<span class='of_step'>step 5 of 7</span>");
			out.println("<div class='clear'></div></div></div>");
			
			stmt.close();
			con.close();
			
			if(id == Channel.WEIBO)
				PrintWeibo(out);
			else if(id == Channel.EMAIL)
				PrintMail(out);
			else if(id == Channel.YMAIL)
				PrintYmail(out);
			
		} catch (IOException | SQLException e) {
			e.printStackTrace();
		}
	}
	
	public static void PrintWeibo_Deep(JspWriter out, int extra)
	{
		try{
			out.println("<div><class = 'clear'></div>");
			out.println("<form method='POST' action='createtaskThat'>");
			out.println("<input type='hidden' name='thatid' value='2'/>");
			out.println("<input type='hidden' name='teveid' value='0'/>");
			
			out.println("<p>weiboUsr<br><input type='text' name='weiboUsr' size='20'></p>");
			out.println("<p>password<br><input type='password' name='password' size='20'></p>");
			out.println("<p>status<br><textarea rows='4' name='status' cols='38'></textarea></p>");
			out.println("<p><div class = 'big_btn'><input type='submit' value='ok' name='B1'/></div></p>");
			out.println("</form>");
			
			out.flush();
		}
		catch(IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	public static void PrintYmail_Deep(JspWriter out, int extra)
	{
		try{
			out.println("<div><class = 'clear'></div>");
			out.println("<form method='POST' action='createtaskThat'>");
			out.println("<input type='hidden' name='thatid' value='4'/>");
			out.println("<input type='hidden' name='teveid' value='0'/>");
			
			out.println("<p>user<br><input type='text' name='user' size='20'>@");
			out.println("<select size='1' name='mailType'>");
			out.println("<option selected>163.com</option>");
			out.println("<option>126.com</option>");
			out.println("<option>yeah.net</option>");
			out.println("</select></p>");
			out.println("<p>pwd<br><input type='password' name='pwd' size='20'></p>");
			out.println("<p>to<br><input type='text' name='to' size='20'></p>");
			out.println("<p>subject<br><input type='text' name='subject' size='20'></p>");
			out.println("<p>body<br><textarea rows='8' name='body' cols='38'></textarea></p>");
			out.println("<p><div class = 'big_btn'><input type='submit' value='ok' name='B1'/></div></p>");
			out.println("</form>");
			
			out.flush();
		}
		catch(IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	public static void PrintMail_Deep(JspWriter out, int extra)
	{
		try{
			out.println("<div><class = 'clear'></div>");
			out.println("<form method='POST' action='createtaskThat'>");
			out.println("<input type='hidden' name='thatid' value='3'/>");
			out.println("<input type='hidden' name='teveid' value='0'/>");
			
			out.println("<p>to<br><input type='text' name='to' size='20'></p>");
			out.println("<p>subject<br><input type='text' name='subject' size='20'></p>");
			out.println("<p>body<br><textarea rows='8' name='body' cols='38'></textarea></p>");
			out.println("<p><div class = 'big_btn'><input type='submit' value='ok' name='B1'/></div></p>");
			out.println("</form>");
			
			out.flush();
		}
		catch(IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	
	public static void Print_Deep(JspWriter out, int id, int extra) throws IOException, SQLException
	{
		try {

			Connection con = Database.getConnection();
			Statement stmt = con.createStatement();
			
			ResultSet rs = stmt.executeQuery(String.format(
						"select tag, pic_url " +
						"from channel " +
						"where id = %d",
						id));
			
			out.println("<div class='clear'></div><br>");
			out.println("<div><div class='step_title'>");
			out.print("<img src=");
			
			
			rs.next();
			out.print("'" + rs.getString("pic_url") + "'");
			out.print(" title = ");
			out.println("'" + rs.getString("tag") + "' style='height:30; width:30'/>");
			out.println("<span class='step_t'>Complete trigger fields</span>" + 
						"<span class='of_step'>step 6 of 7</span>");
			out.println("<div class='clear'></div></div></div>");

			stmt.close();
			con.close();
			
			if(id == Channel.WEIBO)
				PrintWeibo_Deep(out, extra);
			else if(id == Channel.EMAIL)
				PrintMail_Deep(out, extra);
			else if(id == Channel.YMAIL)
				PrintYmail_Deep(out, extra);
			
		} catch (SQLException | IOException e) {
			e.printStackTrace();
		}
	}
	
	static void PrintWeibo(JspWriter out)
	{
		try {
			out.print("<br>");
			out.println("<div><class = 'clear'></div>");
			for(int i = 0; i < 1; i++)
			{
				out.print("<a href='createtaskThat?thatid=2&teveid=" + i + "'>");
				out.print("<div class='individual_trigger'>");
				//title
				if(i == 0)
					out.println("<span class='trigger_t'>Update Status</span>");
				
				//detail
				out.println("<br><span class='trigger_d'>");
				if(i == 0)
					out.println("This action will update an status.");
				
				out.println("</span></div></a>  ");
			}
			out.flush();
		}
		
		catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	static void PrintMail(JspWriter out)
	{
		try {
			out.print("<br>");
			out.println("<div><class = 'clear'></div>");
			for(int i = 0; i < 1; i++)
			{
				out.print("<a href='createtaskThat?thatid=3&teveid=" + i + "'>");
				out.print("<div class='individual_trigger'>");
				//title
				if(i == 0)
					out.println("<span class='trigger_t'>Send me an email</span>");
				
				//detail
				out.println("<br><span class='trigger_d'>");
				if(i == 0)
					out.println("This action will send you an email.");
				
				out.println("</span></div></a>  ");
			}
			out.flush();
		}
		
		catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	static void PrintYmail(JspWriter out)
	{
		try {
			out.print("<br>");
			out.println("<div><class = 'clear'></div>");
			for(int i = 0; i < 1; i++)
			{
				out.print("<a href='createtaskThat?thatid=4&teveid=" + i + "'>");
				out.print("<div class='individual_trigger'>");
				//title
				if(i == 0)
					out.println("<span class='trigger_t'>Send an email</span>");
				
				//detail
				out.println("<br><span class='trigger_d'>");
				if(i == 0)
					out.println("This action will send you an email to\n" + 
								"a single recipient from your Gmail\n" + 
								"account."
							);
				
				out.println("</span></div></a>  ");
			}
			out.flush();
		}
		
		catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

}
